package com.bw.a2209clubshop.ui.mall.viewmodel

import androidx.lifecycle.MutableLiveData
import com.bw.a2209clubshop.base.BaseViewModel
import com.bw.a2209clubshop.base.state.Empty
import com.bw.a2209clubshop.base.state.Failed
import com.bw.a2209clubshop.base.state.Loading
import com.bw.a2209clubshop.base.state.RequestEvent
import com.bw.a2209clubshop.base.state.Success
import com.bw.a2209clubshop.ui.mall.model.MallGoodsDetail
import com.bw.a2209clubshop.ui.mall.reposiitory.MallRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import javax.inject.Inject

@HiltViewModel
class MallGoodsViewModel @Inject constructor(
    private val mRepository: MallRepository
): BaseViewModel() {
    private val _getGoodsDetailEvent = MutableSharedFlow<RequestEvent>()
    val getGoodsDetailEvent = _getGoodsDetailEvent.asSharedFlow()
    val goodsDetailLiveData by lazy {
        MutableLiveData<MallGoodsDetail>()
    }
    fun getGoodsDetail(){
        execute(
            action = mRepository.getGoodsDetail(),
            onError = {
                _getGoodsDetailEvent.emit(Failed(it))
            },
            onStart = {
                _getGoodsDetailEvent.emit(Loading)
            },
            onFinish = {
                if (it.data==null){
                    _getGoodsDetailEvent.emit(Empty)
                }else{
                    _getGoodsDetailEvent.emit(Success)
                    goodsDetailLiveData.postValue(it.data)
                }
            }
        )
    }
}